﻿namespace ElisVK.Data
{
    using System;
    using System.Data;
    using System.Web;

    public class Audio
    {
        private String _fileNameForSave;

        public long AudioID { get; set; }

        public long OwnerID { get; set; }

        public long LyricsID { get; set; }

        public String Artist { get; set; }

        public String Title { get; set; }

        public String DownloadURL { get; set; }

        public int AlbumID { get; set; }

        public Audio() { }

        public Audio(DataRow dataRow)
        {
            long temp;

            long.TryParse(dataRow["aid"].ToString(), out temp);
            AudioID = temp;

            long.TryParse(dataRow["owner_id"].ToString(), out temp);
            OwnerID = temp;

            if (dataRow.Table.Columns.Contains("artist"))
                Artist = HttpUtility.HtmlDecode(dataRow["artist"].ToString());
            
            if (dataRow.Table.Columns.Contains("title"))
                Title = HttpUtility.HtmlDecode(dataRow["title"].ToString());
            
            if (dataRow.Table.Columns.Contains("url"))
                DownloadURL = dataRow["url"].ToString();

            if (dataRow.Table.Columns.Contains("lyrics_id") && !string.IsNullOrEmpty(dataRow["lyrics_id"].ToString()))
                LyricsID = long.Parse(dataRow["lyrics_id"].ToString());
        }

        public String FileNameForSave
        {
            get
            {
                return this._fileNameForSave ?? (this._fileNameForSave = this.Artist + " - " + this.Title + ".mp3");
            }

            set
            {
                this._fileNameForSave = value;
            }
        }


        public override string ToString()
        {
            return Artist + " - " + Title;
        }

        public bool IsEqual(Audio audio)
        {
            if (audio == null)
                return false;

            if (string.Compare(Artist, audio.Artist, true) != 0)
                return false;

            if (string.Compare(Title, audio.Title, true) != 0)
                return false;

            if (string.Compare(DownloadURL, audio.DownloadURL, true) != 0)
                return false;

            return true;
        }
    }
}
